// Variable that gives special rights to the user.
var writer = false;

/**
 * Shows a button for becoming a secretary/writer or not. Calls
 * displayMeetingOneProject(..). Is called by meeting.php.
 */
function displayMeetingProject(projectId) {
    $('body > section').empty();            // Empty the section.
    displayMeetingOneProject(projectId, true, true, true);
    if(writer == false) becomeSecretary(projectId, "Bli sekretær");
    else                becomeSecretary(projectId, "Ikke sekretær");
    
} // end function.

function becomeSecretary(projectId, btnValue){
    $('body > section').append('<button class="meetingInput" \
        name="sekreter" onclick="enableEditMeetingProject(this,'+
        projectId+');" >'+btnValue+'</button>');
}

/**
 * Shows some information about a project, and either displays the employee's
 * comments about the project or displays a new textbox for writing the comments
 * spoken in the meeting.
 */
function displayMeetingOneProject(projectId, showNotes, showStatus, CComments) {
    $.ajax({
        url: 'higface/library/getOneProject.php',
        type : 'POST',
        data: "pid=" + projectId,
        success: function(tmp){ // If the project is found:
            data = eval ('('+tmp+')');
            if(data.error){
                alert(data.error);
                return;
            }

            // Create variables to contain proper information:
            var projectId = data[0][0], owner = data[0][11], title = data[0][2],
            date = data[0][3], shortTitle = data[0][5], description = data[0][6]
            , status = data[0][7], collegiumComments = data[0][8];

            // Display the project's information:
            appendGeneralProjectInfo(title, shortTitle, owner, date);
            if(writer == false && showStatus==true)
                appendProjectStatus(status ,projectId, false);
            appendProjectDescription(description);
            if(CComments==true)
                appendProjectCollegiumComments(collegiumComments);
            
            /*
             * If the employee is not a secretary, the
             * employee's own comments/notes about the project. Else call
             * displayWriteComment(...).
             */
            if(showNotes==true){
                if(writer == false) displayMyNotes(projectId);
                else                displayWriteComment(projectId);
            }
            
        } // end success
    }); // end $.ajax
} // end function

/**
 * This function displays the employees notes for one project
 */
function displayMyNotes(projectId) {
    $.ajax({
        url: 'higface/meeting/library/getNotes.php',
        type: 'POST',
        data: {
            'pid' : projectId
        },
        success: function(tmp) {
            data = eval ('('+tmp+')');
            $('body > section').append('<p id="pNt">Dine notater:</>'); // display the employees notes
            if(data.error) {                                            // if no notes found display error
                $('body > section').append('<div id="projectInfo"><p id="pDate" >'+data.error+'</p></div>');
                return;
            }
            var note = data[0][2].replace(/\n/g,"<br>");    // replace \n to a <br> tag
            $('body > section').append('<div id="projectInfo"><p id="pNote" >'+note+'</p></div>'); // display note
        }
    });
}

/**
 * 
 */
function displayWriteComment(projectId) {    
    getProjectComment(projectId);
}

/**
 * This function creates the opertunity to write comment to a project that all employees can see
 */
function getProjectComment(projectId) {
    $.ajax({
        url: 'higface/meeting/library/getComment.php',
        type: 'POST',
        data: {
            'pid' : projectId
        },
        success: function(tmp) {
            data = eval ('('+tmp+')');            
            displayDropDownStatus(data[0][1], projectId); // displays the status and posibility to set status            
            $('body > section').append('<p id="pWriteComment">Skriv kommentar:</p>');
            if(data.error){ // if there is no comment already created
                $('body > section').append('<textArea id="projectComment"></textArea>');    // display a textare with a button to save comment
                $('body > section').append('<input type="button" class="meetingInput" value="Lagre kommentar" >');
                return;
            }
            $('body > section').append('<textArea id="projectComment">'+data[0][0]+'</textArea>');// display comment to be edited and button to save edit
            $('body > section').append('<button id="myCommentButton" onclick="saveProjectComment('+projectId+');" >Lagre kommentar</button>');
        }
    });
}

/**
 *This function creates a dropdown menu to set a projects status
 */
function displayDropDownStatus(status, projectId){
    var alt = ["need reworking","cleared", "denied"]; // sets the posible choise
            var dropdown = document.createElement("SELECT"); // create the dropdown tag
            dropdown.setAttribute("id", "projectStatus");   // set id
            for(i=0; i<alt.length; i++) {                   // add the choises to the dropdown
                var optn = document.createElement("OPTION");
                if(alt[i] == status){
                    optn.selected = "selected";         // if project is one of the choises set default
                }
                optn.text = alt[i];
                dropdown.options.add(optn);             // add option to dropdown
            }
            $('body > section').append("<p id='StatusHeader'>Status:</p>"); // add header
            $('body > section').append(dropdown);                           // add dropdown and button
            $('body > section').append('<button class="meetingInput" onclick="saveProjectStatus('+projectId+');" >Lagre Status</button>');
}       

/**
 *This function saves the project comment
 */
function saveProjectComment(projectId) {
    var comment = document.getElementById('projectComment').value; // get comment
    $.ajax({
        url: 'higface/meeting/library/saveComment.php',
        type: 'POST',
        data: {
            'pid' : projectId,
            'comment' : comment
        },
        success: function(data) {
            if(data.error){
                alert(data.error);   // if error the return
                return;
            }                           // if success then display a message
            message("Kommentaren er lagret", "");
        }
    });
}

/**
 *This function saves the status of a project, edited by a employee
 */
function saveProjectStatus(projectId) {
    var status = document.getElementById('projectStatus').value; // get status
    $.ajax({
        url: 'higface/meeting/library/saveStatus.php',
        type: 'POST',
        data: {
            'pid' : projectId,
            'status' : status
        },
        success: function(tmp) {       // if success then display a message
            data = eval ('('+tmp+')');
            sendMail("Status for prosjekt "+data[0][1]+" har blitt endret!", data[0][0]);
            message("Status er oppdatert", "");
        }
    });
}

/*
 *This funciton sends a mail to the external user when a project status i changed
 */
function sendMail(body, email) {
    document.location.href="mailto:"+email+"?body="+body+"&subject=Bacheloroppgave HIG";
}

/**
 * This function switches between being the writer and not
 */
function enableEditMeetingProject(button, projectId) {
    writer = (writer == false) ? true : false;  // if not writer become the write
    (writer == false) ? button.value = "Bli sekretær" : button.value = "Ikke sekretær"; //change button text
    if(projectId != null)   displayMeetingProject(projectId); // if inside a project then reload the page
}
